﻿using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.UI;
using System.Text;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:YoutubePlayer runat=server></{0}:YoutubePlayer>")]
    public class YoutubePlayer : WebControl
    {
        public string Url { get { object obj = ViewState["Url"]; return (obj == null ? "" : obj.ToString()); } set { ViewState["Url"] = value; } }
        public string Title { get { object obj = ViewState["Title"]; return (obj == null ? "YouTube video player" : obj.ToString()); } set { ViewState["Title"] = value; } }
        public string VideoTag { get { return GetVideoTag(); } }
        public bool AutoPlay { get { object obj = ViewState["AutoPlay"]; return (obj == null) ? true : Convert.ToBoolean(obj); } set { ViewState["AutoPlay"] = value; } }
        public bool FullScreen { get { object obj = ViewState["FullScreen"]; return (obj == null) ? false : Convert.ToBoolean(obj); } set { ViewState["FullScreen"] = value; } }
        public bool ShowRelated { get { object obj = ViewState["ShowRelatedContentAtEnd"]; return (obj == null) ? false : Convert.ToBoolean(obj); } set { ViewState["ShowRelatedContentAtEnd"] = value; } }
        public Int32 LoopCount { get { object obj = ViewState["LoopCount"]; return (obj == null) ? 0 : Convert.ToInt32(obj); } set { ViewState["LoopCount"] = value; } }
        public VideoFormat VideoFormat { get { object obj = ViewState["VideoFormat"]; return (obj == null) ? VideoFormat.FLVStandard : (VideoFormat)obj; } set { ViewState["VideoFormat"] = value; } }
        protected override void Render(HtmlTextWriter writer)
        {
            Unit width = this.Width;
            Unit height = this.Height;

            writer.AddAttribute("title", this.Title);
            AddSizeAttribute(writer);

            writer.AddAttribute("id", this.ClientID);
            string url = string.Empty;

            if (FullScreen)
            {
                url = string.Format("http://www.youtube.com/v/{0}?1=1", VideoTag);
            }
            else
            {
                url = string.Format("http://www.youtube.com/v/{0}?1=1", VideoTag);
            }

            if (AutoPlay) { url += "&autoplay=1"; }
            if (LoopCount > 0) { url += "&loop=" + LoopCount.ToString(); }
            if (!ShowRelated) { url += "&rel=0"; }
            url += "&fmt=" + Convert.ToInt32(this.VideoFormat).ToString();

            writer.AddAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
            writer.AddAttribute("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0");
            //writer.AddAttribute("codebase","http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0");

            AddSizeAttribute(writer);


            writer.AddAttribute("id", this.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Object);

            AddParameter(writer, "movie", url);
            AddParameter(writer, "quality", "autohigh");
            writer.AddAttribute("href", url, false);
            writer.AddAttribute("quality", "autohigh");

            AddSizeAttribute(writer);

            writer.AddAttribute("align", string.Empty);
            writer.AddAttribute("name", this.ClientID);
            writer.AddAttribute("type", "application/x-shockwave-flash");
            writer.AddAttribute("pluginspace", "http://www.macromedia.com/go/getflashplayer");
            writer.RenderBeginTag(HtmlTextWriterTag.Embed);
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        private void AddSizeAttribute(HtmlTextWriter writer)
        {
            if ((this.Width.IsEmpty) || (this.Height.IsEmpty))
            {
                switch (this.VideoFormat)
                {
                    case Core.Controls.VideoFormat.FLVLow:
                        writer.AddAttribute("width", "320"); writer.AddAttribute("height", "240"); break;
                    case Core.Controls.VideoFormat.FLVLowMono:
                        writer.AddAttribute("width", "320"); writer.AddAttribute("height", "240"); break;
                    case Core.Controls.VideoFormat.FLVLowMono22:
                        writer.AddAttribute("width", "320"); writer.AddAttribute("height", "240"); break;
                    case Core.Controls.VideoFormat.FLVSmall:
                        writer.AddAttribute("width", "176"); writer.AddAttribute("height", "144"); break;
                    case Core.Controls.VideoFormat.HD1080:
                        writer.AddAttribute("width", "1920"); writer.AddAttribute("height", "1080"); break;
                    case Core.Controls.VideoFormat.HD720:
                        writer.AddAttribute("width", "1280"); writer.AddAttribute("height", "720"); break;
                    case Core.Controls.VideoFormat.MP4:
                        writer.AddAttribute("width", "640"); writer.AddAttribute("height", "480"); break;
                    case Core.Controls.VideoFormat.FLVStandard:
                    default:
                        writer.AddAttribute("width", "640"); writer.AddAttribute("height", "480"); break;
                }
            }
            else
            {
                writer.AddAttribute("width", this.Width.Value.ToString());
                writer.AddAttribute("height", this.Height.Value.ToString());
            }


        }


        private string GetVideoTag()
        {
            if (string.IsNullOrEmpty(this.Url)) { return string.Empty; }

            Regex regex = new Regex("v=(?<video>\\w*)", RegexOptions.IgnoreCase);
            if (!regex.IsMatch(this.Url)) { return string.Empty; }

            Match m = regex.Match(this.Url);

            if (m == null) { return string.Empty; }

            if (m.Groups["video"] != null)
            {
                return m.Groups["video"].Value;
            }

            return string.Empty;
        }

        private void AddParameter(HtmlTextWriter output, string name, string value)
        {
            output.AddAttribute("name", name);
            output.AddAttribute("value", value, false);
            output.RenderBeginTag(HtmlTextWriterTag.Param);
            output.RenderEndTag();

        }

    }

    public enum VideoFormat
    {
        HD1080 = 37,
        HD720 = 22,
        MP4 = 35,
        FLVStandard = 18,
        FLVLow = 34,
        FLVLowMono = 6,
        FLVLowMono22 = 5,
        FLVSmall = 17
    }

}
